package Utils;

import entity.Student;
import entity.User;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author:wmx 2025-03-19 09:57
 * 1、功能:
 * 2、参数{
 * <p>
 * <p>
 * }
 **/
public class DbUtil {
    static  DruidUtil druidUtil = new DruidUtil();
    private static Connection connection = null;
    static {
        connection = druidUtil.getConnection();
    }
    public static List<?> select4Bean(Class<?> aClass, Map<String,Object> cond) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //拼接sql语句
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("select ");
        //列拼接
        for (Map.Entry<String,Object> entry: cond.entrySet()){

            stringBuilder.append(entry.getKey()+",");
        }
        //删除列名最后一个逗号
        stringBuilder.delete(stringBuilder.length()-1,stringBuilder.length());
        stringBuilder.append(" ");


        //类注解
        if (aClass.isAnnotationPresent(javax.persistence.Table.class)) {
            javax.persistence.Table tableAnnotation = aClass.getAnnotation(javax.persistence.Table.class);
            String tableName = tableAnnotation.name();
            System.out.println("数据库表名: " + tableName);
            stringBuilder.append("from "+tableName);
        }
        stringBuilder.append(" where ");


        //条件拼接
        for (Map.Entry<String,Object> entry: cond.entrySet()){
                stringBuilder.append(entry.getKey()+" = ? and ");
        }
        stringBuilder.delete(stringBuilder.length()-4,stringBuilder.length());

        //实例化实体类


        //==================数据表操作=======================
        /**
         * 查询
         */
        List<Student> list = new ArrayList<Student>() ;
        String str = stringBuilder.toString();
        System.out.println(str);

        try {
            int j = 1;
            PreparedStatement preparedStatement = connection.prepareStatement(str);
            for (Map.Entry<String,Object> entry: cond.entrySet()){
                preparedStatement.setObject(j,entry.getValue());
                j++;
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Student student = new Student(name, id, 0.0);
                list.add(student);

            }


        } catch (SQLException e) {
            e.printStackTrace();
        }


        return list;
    }


    public static int insert(User user) throws NoSuchMethodException, IllegalAccessException, SQLException {
        StringBuilder stringBuilder = new StringBuilder();
        Class<? extends User> aClass = user.getClass();

        Field[] declaredFields = aClass.getDeclaredFields();

        stringBuilder.append("insert into user values ( ");
        //反射属性值
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            stringBuilder.append("?, ");
            Object value = declaredField.get(user);
        }

        stringBuilder.delete(stringBuilder.length()-2,stringBuilder.length());
        stringBuilder.append(" ) ");
        String sql = stringBuilder.toString() ;




        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        int i = 1;
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            preparedStatement.setObject(i,declaredField.get(user));
            i++;
        }


        return   preparedStatement.executeUpdate();
    }



}
